package tubesprogin4;

import java.io.File;
import java.net.URI;
import java.nio.ByteBuffer;
import mudus.GlobalSettings;

public class ClientMessage {
    
    public static byte[] createRequestMetafileMsg(String filepath, String filename) {
        int type = GlobalSettings.REQUEST_METAFILE; 
        String msg = filepath + "|" + filename;
        byte[] sb = msg.getBytes();
        byte[] bmsg = new byte[1 + msg.length()];
        System.arraycopy(sb, 0, bmsg, 1, sb.length);
        bmsg[0] = (byte) type;
        return bmsg;
    }
    
    public static byte[] createRequestFilePieceMsg(String filepath, String filename, int ipiece) {
        int type = GlobalSettings.REQUEST_FILEPIECE;
        String msg = filepath + "|" + filename + "|" + ipiece;
        byte[] sb = msg.getBytes();
        byte[] bmsg = new byte[1 + msg.length()];
        System.arraycopy(sb, 0, bmsg, 1, sb.length);
        bmsg[0] = (byte) type;
        return bmsg;
    }
    
    public static byte[] createEndRequestFilePiece() {
        int type = GlobalSettings.END_REQUEST_FILEPIECE;
        byte[] bmsg = new byte[1];
        bmsg[0] = (byte) type;
        return bmsg;
    }
    
    public static byte[] createUploadMetaFileMsg(File file) {                
        long filesize = file.length();
        int npiece = (int) Math.ceil(filesize * 1.0d / GlobalSettings.PIECE_LENGTH);
        File clientRepo = new File(ClientSettings.CLIENT_REPO_PATH);
        URI base = clientRepo.toURI();
        String relativePath = base.relativize(file.getParentFile().toURI()).getPath();
        
        StringBuilder sb = new StringBuilder();        
        sb.append(relativePath).append("|");
        sb.append(file.getName()).append("|");
        sb.append(filesize).append("|");
        sb.append(GlobalSettings.PIECE_LENGTH).append("|");        
        sb.append(npiece).append("|");
        byte[] meta = sb.toString().getBytes();
        
        int type = GlobalSettings.START_UPLOAD_FILE;
        byte[] bmsg = new byte[meta.length + 1];
        bmsg[0] = (byte) type;
        
        System.arraycopy(meta, 0, bmsg, 1, meta.length);
        
        return bmsg;
    }
    
    public static byte[] createUploadFilePieceMsg(int ipiece, byte[] data) {
        int type = GlobalSettings.UPLOAD_FILEPIECE;        
        
        ByteBuffer bb = ByteBuffer.allocate(4);
        bb.putInt(ipiece);                       
        byte[] bipiece = bb.array();
                        
        byte[] ret = new byte[bipiece.length + data.length + 1];
        
        ret[0] = (byte) type;
        System.arraycopy(bipiece, 0, ret, 1, bipiece.length);        
        System.arraycopy(data, 0, ret, bipiece.length + 1,data.length);                                
        return ret;
    }
    
    public static byte[] createEndUploadFile() {
        int type = GlobalSettings.END_UPLOAD_FILE;
        byte[] bmsg = new byte[1];
        bmsg[0] = (byte) type;
        return bmsg;
    }
    
    public static byte[] createRequestMetaLogFile() {
        int type = GlobalSettings.REQUEST_METALOGFILE;
        byte[] bmsg = new byte[1];
        bmsg[0] = (byte) type;
        return bmsg;
    }
    
    public static byte[] createRequestLogFilePieceMsg(int ipiece) {
        int type = GlobalSettings.REQUEST_LOGFILEPIECE;        
        String msg = ""+ipiece;
        byte[] sb = msg.getBytes();
        byte[] bmsg = new byte[1 + msg.length()];
        System.arraycopy(sb, 0, bmsg, 1, sb.length);
        bmsg[0] = (byte) type;
        return bmsg;
    }
    
    public static byte[] createEndRequestLogFilePieceMsg() {
        int type = GlobalSettings.END_REQUEST_LOGFILEPIECE;
        byte[] bmsg = new byte[1];
        bmsg[0] = (byte) type;
        return bmsg;
    }
    
    public static byte[] createRequestDeleteOnServerMsg(String filepath, String filename) {
        int type = GlobalSettings.REQUEST_DELETE_ON_SERVER;
        String msg = filepath + "|" + filename;
        byte[] sb = msg.getBytes();
        byte[] bmsg = new byte[1 + msg.length()];
        System.arraycopy(sb, 0, bmsg, 1, sb.length);
        bmsg[0] = (byte) type;
        return bmsg;
    }
}
